<template>
  <div>
    <h1>购买商品</h1>
    <div style="margin-bottom: 20px">
      <el-descriptions
        class="margin-top"
        title="商品信息"
        :column="3"
        :size="size"
        border
      >
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">商品名称</div>
          </template>
          {{ prod.prodName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">商品类型</div>
          </template>
          {{ prod.typeName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">商品品牌</div>
          </template>
          {{ prod.brandName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">商品单位</div>
          </template>
          <el-tag size="small"> {{ prod.unitName }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">商品供应商</div>
          </template>
          {{ prod.apprName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">商品图片</div>
          </template>
          <el-image style="width: 100px; height: 100px" :src="prod.prodImage" />
        </el-descriptions-item>
      </el-descriptions>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="infoId" label="ID" />
        <el-table-column prop="prodSku" label="SKU" />
        <el-table-column prop="prodName" label="商品ID" />
        <el-table-column prop="prodStock" label="库存" />
        <el-table-column prop="alertStock" label="预警库存" />
        <el-table-column prop="prodPrice" label="售价" />
        <el-table-column label="操作" />
      </el-table>
      <div>
        <el-pagination
          v-model:current-page="searchForm.pageNum"
          v-model:page-size="searchForm.pageSize"
          :page-sizes="[2, 4, 6, 8]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getProdInfo } from "@/api/api";
import { computed } from "vue";
import {
  Iphone,
  Location,
  OfficeBuilding,
  Tickets,
  User,
} from "@element-plus/icons-vue";
import type { ComponentSize } from "element-plus";

const size = ref<ComponentSize>("default");
const prod = ref({});

const tableData = ref([]);
const total = ref(0);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  typeId: null,
});
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadProdInfoList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadProdInfoList();
};
const loadProdInfoList = () => {
  getProdInfo(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  let s = sessionStorage.getItem("buy");
  prod.value = JSON.parse(s);
  searchForm.value.typeId = prod.value.prodId;
  console.log("typeId", prod.value.prodId);
  console.log("prodInfo", prod.value);

  loadProdInfoList();
});
</script>

<style scoped></style>
